﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using PMQLCHH.BUS;
using PMQLCHH.DAO;
using PMQLCHH.DTO;

namespace PMQLCHH.GUI
{
    public partial class MATHANG : DevComponents.DotNetBar.Office2007Form
    {
        //Kha Nguyen - Giao Dien Mat hang - 5/6/2013 - begin
        public MATHANG()
        {
            InitializeComponent();
            //btn_capnhat.Enabled = false;
            rbt_TrangThai_Bat.Checked = true;
            pn_MaMatHang.Hide();
            lblMaMatHang.Text = "";
        }

        //event 
        private void MATHANG_Load(object sender, EventArgs e)
        {
            NapDanhSachMatHang();
            NapDanhSachLoaiMatHang();
            NapDanhSachDonViTinh();
            NapTiemKiemLoaiMatHang();
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            if (String.Compare(btn_them.Text, "Thêm") == 0)
            {
                ThemMatHang();
            }
            else
            {
                if (String.Compare(btn_them.Text, "Cập Nhật") == 0)
                {
                    CapNhatMatHang();
                }
            }
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            DongMatHang();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            DialogResult KQ = MessageBox.Show("Bạn có chắc muốn xóa! \nDữ liệu sau khi xóa không thể phục hồi. Tiếp tục xóa?", "Xóa", MessageBoxButtons.YesNo);
            if (KQ == DialogResult.Yes)
            {
                XoaNhieuMatHang();
                NapDanhSachMatHang();
            }
        }

        private void btn_lammoi_Click(object sender, EventArgs e)
        {
            LamMoiNhapLieu();
        }



        private void btn_timkiem_Click(object sender, EventArgs e)
        {
            TimKiemMatHang();
        }

        private void btn_XuatDL_Click(object sender, EventArgs e)
        {
            XuatRaTapTin();
        }

        private void btn_NhapDL_Click(object sender, EventArgs e)
        {
            NhapTuTapTin();
        }
        private void btn_NapLai_Click(object sender, EventArgs e)
        {
            NapMatHangCanChinhSua();
            btn_them.Text = "Cập Nhật";
            pn_MaMatHang.Show();
        }

        private void txtTimKiem_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_timkiem.PerformClick();
            }
        }

        //methods

        /// <summary>
        /// Nap Danh sach Mat hang Da Co
        /// </summary>
        /// 
        public void NapDanhSachMatHang()
        {
            try
            {
                MatHangBUS MHBus = new MatHangBUS();
                DataTable dt = MHBus.LayDanhSachMatHang();
                if (dt.Rows.Count > 0)
                {
                    dtg_DanhSachMatHang.DataSource = dt;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Lam moi cac o nhap lieu
        /// </summary>
        ///
        public void LamMoiNhapLieu()
        {
            try
            {
                txt_GhiChu.Text = "";
                txt_TenMatHang.Text = "";
                rbt_TrangThai_Bat.Checked = true;
                cbb_DonViTinh.SelectedIndex = 0;
                cbb_LoaiMatHang.SelectedIndex = 0;
                btn_them.Text = "Thêm";
                pn_MaMatHang.Hide();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Nap Thong tin can chinh sua
        /// </summary>
        ///
        public void NapMatHangCanChinhSua()
        {
            try
            {
                MatHangBUS MHbus= new MatHangBUS();
                lblMaMatHang.Text = dtg_DanhSachMatHang.CurrentRow.Cells["MaMatHang"].Value.ToString();
                txt_GhiChu.Text = dtg_DanhSachMatHang.CurrentRow.Cells["GhiChu"].Value.ToString();
                txt_TenMatHang.Text = dtg_DanhSachMatHang.CurrentRow.Cells["TenMatHang"].Value.ToString();
                string TrangThai = dtg_DanhSachMatHang.CurrentRow.Cells["TrangThai"].Value.ToString();
                if (String.Compare(TrangThai, "Bật") == 0)
                {
                    rbt_TrangThai_Bat.Checked = true;
                }
                else
                {
                    rbt_TrangThai_Tat.Checked = true;
                }

                string DonViTinh = dtg_DanhSachMatHang.CurrentRow.Cells["TenDonVi"].Value.ToString();
                cbb_DonViTinh.SelectedValue = MHbus.LayMaDonViTinh(DonViTinh);
                string LoaiMatHang = dtg_DanhSachMatHang.CurrentRow.Cells["TenLoaiMatHang"].Value.ToString();
                cbb_LoaiMatHang.SelectedValue = MHbus.LayMaLoaiMatHang(LoaiMatHang); 
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Nap Danh sach chon loai mat hang
        /// </summary>
        /// 
        public void NapDanhSachLoaiMatHang()
        {
            try
            {
                MatHangBUS MHBus = new MatHangBUS();
                DataTable dt = MHBus.LayDanhSachLoaiMatHang();
                if (dt.Rows.Count > 0)
                {
                    cbb_LoaiMatHang.DataSource = dt;
                    cbb_LoaiMatHang.ValueMember = "MaLoaiMatHang";
                    cbb_LoaiMatHang.DisplayMember = "TenLoaiMatHang";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Nap Danh sach chon don vi tinh
        /// </summary>
        /// 
        public void NapDanhSachDonViTinh()
        {
            try
            {
                MatHangBUS MHBus = new MatHangBUS();
                DataTable dt = MHBus.LayDanhSachDonViTinh();
                if (dt.Rows.Count > 0)
                {
                    cbb_DonViTinh.DataSource = dt;
                    cbb_DonViTinh.ValueMember = "MaDonVi";
                    cbb_DonViTinh.DisplayMember = "TenDonVi";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Nap Danh sach loai mat hang cho chuc nang tim kiem 
        /// </summary>
        /// 
        public void NapTiemKiemLoaiMatHang()
        {
            try
            {

                
                MatHangBUS MHBus = new MatHangBUS();
                DataTable dt = MHBus.LayDanhSachLoaiMatHang();
                int iSoDong = dt.Rows.Count;
                DataTable dtV = new DataTable();
                dtV.Columns.Add("MaLoaiMatHang");
                dtV.Columns.Add("TenLoaiMatHang");
                DataRow newCustomersRow = dtV.NewRow();
                newCustomersRow["MaLoaiMatHang"] = 0;
                newCustomersRow["TenLoaiMatHang"] = "Tất Cả";
                dtV.Rows.Add(newCustomersRow);
                if (iSoDong > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string strTemps = dr["TenLoaiMatHang"].ToString();
                        int iTemp = (int)dr["MaLoaiMatHang"];
                        newCustomersRow = dtV.NewRow();
                        newCustomersRow["MaLoaiMatHang"] = iTemp;
                        newCustomersRow["TenLoaiMatHang"] = strTemps;
                        dtV.Rows.Add(newCustomersRow);
                    }
                }
                ccbTimKiem.DataSource = dtV;
                ccbTimKiem.ValueMember = "MaLoaiMatHang";
                ccbTimKiem.DisplayMember = "TenLoaiMatHang";
                ccbTimKiem.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Them Mat hang
        /// </summary>
        /// 
        public void ThemMatHang()
        {
            if (!(txt_TenMatHang.Text == ""))
            {
                try
                {
                    MatHangBUS MHBus = new MatHangBUS();
                    MatHangDTO MHDto = new MatHangDTO();
                    MHDto.TenMatHang = txt_TenMatHang.Text;
                    MHDto.LoaiMatHang = int.Parse(cbb_LoaiMatHang.SelectedValue.ToString());
                    MHDto.DonViTinh = int.Parse(cbb_DonViTinh.SelectedValue.ToString());

                 
                    //bool result;
                    float GiaThamKhao = 0;
                    //result = float.TryParse(txt_GiaThamKhao.Text,out GiaThamKhao);
                    //if (!result)
                    //{
                    //    GiaThamKhao = 0;
                    //}
                    MHDto.GiaThamKhao = GiaThamKhao;
                    MHDto.Ghichu_MH = txt_GhiChu.Text;
                    if (rbt_TrangThai_Bat.Checked)
                    {
                        MHDto.TrangThai = 1;
                    }
                    else
                    {
                        MHDto.TrangThai = 0;
                    }
                    //MHDto.NgayLap = DateTime.Now;
                    MHDto.SoLuongTon = 0;
                    
                    bool KQ = MHBus.ThemMatHang(MHDto);
                    LamMoiNhapLieu();
                    NapDanhSachMatHang();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }


        /// <summary>
        /// cap nhat mat hang
        /// </summary>
        /// 
        public void CapNhatMatHang()
        {
            if (!(txt_TenMatHang.Text == ""))
            {
                try
                {
                    MatHangBUS MHBus = new MatHangBUS();
                    MatHangDTO MHDto = new MatHangDTO();
                    MHDto.MaMatHang = lblMaMatHang.Text;
                    MHDto.TenMatHang = txt_TenMatHang.Text;
                    MHDto.LoaiMatHang = int.Parse(cbb_LoaiMatHang.SelectedValue.ToString());
                    MHDto.DonViTinh = int.Parse(cbb_DonViTinh.SelectedValue.ToString());


                    //bool result;
                    //float GiaThamKhao = 0;
                    //result = float.TryParse(txt_GiaThamKhao.Text, out GiaThamKhao);
                    //if (!result)
                    //{
                    //    GiaThamKhao = 0;
                    //}
                    //MHDto.GiaThamKhao = GiaThamKhao;
                    MHDto.Ghichu_MH = txt_GhiChu.Text;
                    if (rbt_TrangThai_Bat.Checked)
                    {
                        MHDto.TrangThai = 1;
                    }
                    else
                    {
                        MHDto.TrangThai = 0;
                    }
                    //MHDto.NgayLap = DateTime.Now;
                    //MHDto.SoLuongTon = 0;

                    bool KQ = MHBus.CapNhatMatHang(MHDto);
                    LamMoiNhapLieu();
                    NapDanhSachMatHang();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }





        /// <summary>
        /// xoa mat hang
        /// </summary>
        /// 
        public void XoaNhieuMatHang()
        {
            int count = 0;
            bool flag = false;
            foreach (DataGridViewRow row in dtg_DanhSachMatHang.Rows)
            {
                DataGridViewCheckBoxCell chk_chon = row.Cells["Xoa"] as DataGridViewCheckBoxCell;

                if (Convert.ToBoolean(chk_chon.Value) == true)
                {
                    
                    MatHangBUS mh = new MatHangBUS();
                    string MaMH = row.Cells["MaMatHang"].Value.ToString();
                    
                    flag = mh.DaDatHoaDon(MaMH);
                    if (!flag)
                    {
                        flag = mh.DaDatPhieuNhap(MaMH);
                    }
                    if (!flag)
                    {
                        flag = mh.DaDatPhieuXuat(MaMH);
                    }

                    if (!flag)
                    {
                        mh.XoaMatHang(MaMH);
                        count++;
                    }                    
                }
            }
            if (count != 0)
            {
                MessageBox.Show("Đã xóa thành công " + count.ToString() + " mặt hàng!", "Thông Báo");
            }
            else if(!flag)
            {
                MessageBox.Show("không có mặt hàng được chọn!", "Thông Báo");
            }
            else if (flag)
            {
                MessageBox.Show("Không thể xóa mặt hàng!", "Thông Báo");
            }
        }

        /// <summary>
        /// dong mat hang
        /// </summary>
        /// 

        public void DongMatHang()
        {
            this.Close();
        }

        /// <summary>
        /// Tim Kiem Mat hang
        /// </summary>
        /// 

        public void TimKiemMatHang()
        {
            string strTuKhoa = txtTimKiem.Text;
            int iLoaiMatHang = int.Parse(ccbTimKiem.SelectedValue.ToString());
            MatHangBUS MHbus = new MatHangBUS();
            dtg_DanhSachMatHang.DataSource = MHbus.TimKiem(strTuKhoa, iLoaiMatHang);
        }


        /// <summary>
        /// xuat ra tap tin excel
        /// </summary>
        /// 
        public void XuatRaTapTin()
        {
            SaveFileDialog f = new SaveFileDialog();
            f.Filter = "Excel WorkBook |*.*";
            if (f.ShowDialog() == DialogResult.OK)
            {
                MatHangBUS MHBus= new MatHangBUS();
                DataTable dt = new DataTable();
                dt = MHBus.LayBangMatHang();
                string path = System.IO.Path.GetDirectoryName(f.FileName);
                string onlyFileName = System.IO.Path.GetFileNameWithoutExtension(f.FileName);
                MHBus.XuaTMatHangRaExcel(dt,path + "\\" + onlyFileName);
                MessageBox.Show("Xuất tập dữ liệu thành công!", "Thông Báo");
            }
            
        }

        /// <summary>
        /// nhap tu tap tin excel
        /// </summary>
        /// 
        public void NhapTuTapTin()
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Filter = "Excel WorkBook |*.xlsx| Excel 97-2003 WorkBook |*.xls";
            o.FilterIndex = 1;
            if (o.ShowDialog() == DialogResult.OK)
            {
                MatHangBUS MHBus = new MatHangBUS();
                int SoLuongNhap = MHBus.NhapTuExcel(o.FileName);
                MessageBox.Show("Đã thêm "+SoLuongNhap+" record thành công!", "Thông Báo");
            }

            NapDanhSachMatHang();
        }


        //Kha Nguyen - Giao Dien Mat hang - 5/6/2013 - end
    }

}